//
//  CZUserAccountTools.swift
//  weibo
//
//  Created by 丁蒙 on 16/6/26.
//  Copyright © 2016年 丁蒙. All rights reserved.
//

import UIKit

class CZUserAccountTools: NSObject {
    //路径
    let path = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archeiver")
    
    //用户对象
    var userAccount: CZUserAcount?
    //访问令牌
    var access_token: String?{
        if userAccount?.access_token == nil {
            return nil
        } else {
            if userAccount?.expires_date?.compare(NSDate()) == NSComparisonResult.OrderedDescending  {
                return userAccount?.access_token
            } else {
                return nil
            }
        }
        
    }
    //用户是否登录
    var isLogin: Bool {
        
        return access_token != nil
    }
    
    //全局访问点 单例
    static let sharedUserAccountTools: CZUserAccountTools = CZUserAccountTools()
    
    //保存用户
    func saveUserAccount(userAccount: CZUserAcount) {
        self.userAccount = userAccount
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: path)
    }
    
    //取出
    func getUserAccount() -> CZUserAcount? {
        let userAccount = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? CZUserAcount
        return userAccount
        
    }
    
    override init() {
        super.init()
        userAccount = getUserAccount()
    }
}
